package ru.amse.tsyganov.jumleditor.commands;

import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.model.Partition;
import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.view.ActivityGraphView;
import ru.amse.tsyganov.jumleditor.view.StateView;

/*package*/ class NewStateCommand extends Command {
	
	private final ActivityGraphView<? extends ActivityGraph> activityGraphView;
	
	private final StateView<? extends State> stateView;
	
	public NewStateCommand(
			ActivityGraphView<? extends ActivityGraph> activityGraphView,
			StateView<? extends State> stateView) {
		
		if (activityGraphView == null || stateView == null) {
			throw new IllegalArgumentException();
		}
		
		this.activityGraphView = activityGraphView;
		this.stateView = stateView;
	}
	
	@Override
	public void execute() {
		activityGraphView.getModelViewMap().getStatesMap().
			put(stateView.getModel(), stateView);
		
		ActivityGraph activityGraph = activityGraphView.getModel();
		
		for (Partition p : activityGraph.getPartitions()) {
			p.addState(stateView.getModel());
			break;
		}	
	}

	@Override
	public void unexecute() {
		activityGraphView.getModelViewMap().getStatesMap().
			remove(stateView.getModel());
		
		ActivityGraph activityGraph = activityGraphView.getModel();
		
		for (Partition p : activityGraph.getPartitions()) {
			if(p.removeState(stateView.getModel())) {
				break;
			}
		}	
	}
}